package widget.dress.manager
{
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	import widget.common.tools.ClassLoader;
	import widget.common.utils.MyLog;
	import widget.dress.DressViewer;
	import widget.dress.enum.ActionEnum;
	import widget.dress.enum.DressEnum;
	import widget.dress.mode.ActionModel;
	import widget.dress.mode.CutModel;
	import widget.dress.mode.DressModel;
	import widget.dress.tool.CartoonTimer;
	import widget.dress.tool.DressCutUtil;

	public class DressManager
	{
		private static var _instance:DressManager = null;
		
		//排队需要加载对象数据对象集合
		private var loadList : Vector.<Object> = null;
		
		//开始加载或者已经加载完成的数据对象集合
		private var loadedList:Vector.<Object> = null;
		
		private var timerList:Vector.<CartoonTimer> = null;
		
		//已经加载的图片模板库
		private var loadedDressDic : Dictionary = null;
		
		private var isLoaded:Boolean = false;

		public function DressManager()
		{
			if(_instance)
				throw new Error("dress manager is singleton!!!!");
			init();
		}
		
		public static function getInstance():DressManager
		{
			if(_instance == null)
				_instance = new DressManager();
			return _instance;
		}
		
		private function init():void
		{
			loadList = new Vector.<Object>;
			loadedDressDic = new Dictionary();
			timerList = new Vector.<CartoonTimer>;
			loadedList = new Vector.<Object>;
		}
		
		public function addDress(mode:DressModel,viewer:DressViewer):void
		{
			var data:Object = {uid:mode.uid,model:mode,viewer:viewer};
			loadList.push(data);
			updateDress();
		}
		
		private function createTimer(mode:DressModel,viewer:DressViewer):CartoonTimer
		{
			if(mode == null || viewer == null) return null;
			var timer:CartoonTimer = new CartoonTimer(mode, viewer, ActionEnum.FRAME_ACTION_TIME);
			timer.setCurrentStatus(1,0,0);	
			timer.autoPlay = true;
			return timer;
		}
		
		private function updateDress() : void
		{
			if(isLoaded) return;
			
			if (this.loadList.length > 0)
			{
				var tempObj : Object = this.loadList.shift();
				loadedList.push(tempObj);
				var model:DressModel = tempObj.model;
				if (model.updateID == "")
				{
					tempObj.model.updateID = DressEnum.APPEARANCE_USER_MALE;
				}
				if (tempObj.viewer && tempObj.viewer.dataList)
				{
					var list:Vector.<Vector.<CutModel>> = loadedDressDic[tempObj.model.updateID]
					if (list != null)
					{
						isLoaded = false;
						fillTemplate(tempObj.uid,list);
						updateDress();
						timerList.push(createTimer(tempObj.model,tempObj.viewer));
					}
					else //加载图片
					{
						isLoaded = true;
						loadDepot(tempObj.uid,tempObj.model.updateID)
					}
				}else{
					isLoaded = true;
					updateDress();
				}
			}
		}

		private function loadDepot(uid:String,dressID : String) : void
		{
			var loaderURL : String = null;
			if (dressID == DressEnum.APPEARANCE_USER_FEMALE)
			{
				loaderURL = "resource/depot/appear_female.swf"
			}
			else if (dressID == DressEnum.APPEARANCE_NPC)
			{
				loaderURL = "resource/depot/appear_npc.swf";
			}
			else if (dressID == DressEnum.APPEARANCE_USER_MALE)
			{
				loaderURL = "resource/depot/appear_male.swf";
			}
			if(loaderURL == null)
				return;
			var loader : ClassLoader = new ClassLoader();
			loader.uid = uid;
			loader.loaderID = dressID;
			loader.loadByURL(loaderURL);
			loader.addEventListener(Event.COMPLETE , depotCompleteHandle);
			loader.addEventListener(IOErrorEvent.IO_ERROR , depotErrorHandle);
		}

		private function depotCompleteHandle(e : Event) : void
		{
			var temp : ClassLoader = e.currentTarget as ClassLoader;
			dealLoadedImage(temp);
		}

		private function depotErrorHandle(e : IOErrorEvent) : void
		{
			MyLog.debug("error depot by Appearance");
		}

		private function dealLoadedImage(loader : ClassLoader) : void
		{
			var dressArray :Vector.<Vector.<CutModel>> = new Vector.<Vector.<CutModel>>();
			
			var class1:Class = loader.getClassByName(DressEnum.TYPE_DRESS_STOP);
			dressArray.push(DressCutUtil.getCut(new class1() , 150 , 200));

			var class2:Class = loader.getClassByName(DressEnum.TYPE_DRESS_MOVE);
			dressArray.push(DressCutUtil.getCut(new class2() , 150 , 200));

			/* tempClass = loader.GetClass("ready",loader.loadinfo);
			   tempArray = CutCharacter.cut(new tempClass(),110,200);
			   tempEquipArray[AppearanceConfig.MOVE_BY_READY] = tempArray;

			   tempClass = loader.GetClass("attack",loader.loadinfo);
			   tempArray = CutCharacter.cut(new tempClass(),110,200);
			   tempEquipArray[AppearanceConfig.MOVE_BY_FIGHT] = tempArray;

			   tempClass = loader.GetClass("magice",loader.loadinfo);
			   tempArray = CutCharacter.cut(new tempClass(),110,200);
			 tempEquipArray[AppearanceConfig.MOVE_BY_MAGICE] = tempArray; */

			loadedDressDic[loader.loaderID] = dressArray;
			fillTemplate(loader.uid,dressArray);
			var timer:CartoonTimer = createTimer(getModelByUID(loader.uid),getViewerByUID(loader.uid));
			if(timer)
				timerList.push(timer);
			
			loader.clear();
			loader = null;
		}
		
		private function fillTemplate(uid:String,template:Vector.<Vector.<CutModel>>):void
		{
			var viewer:DressViewer = getViewerByUID(uid);
			if(viewer == null) return;
			var temp:Vector.<ActionModel> = viewer.dataList;
			var list:Vector.<CutModel> = null;
			var cutMode:CutModel = null;
			temp.splice(0,temp.length);
			for(var i:uint = 0;i < template.length;i++){
				list = template[i] as Vector.<CutModel>;
				if(list){
					var actData:ActionModel = new ActionModel();
					actData.action = i;
					for(var j:uint = 0;j < list.length;j++){
						cutMode = list[j] as CutModel;
						var dirData:ActionModel = new ActionModel();
						dirData.direction = j;
						for(var k:uint = 0;k < cutMode.length;k++){
							var mode:ActionModel = new ActionModel();
							mode.action = i;
							mode.direction = j;
							mode.frame = k;
							mode.bitmap = new Bitmap(cutMode.getCloneData(k));
							dirData.addModel(mode);
						}
						actData.addModel(dirData);
					}
					temp.push(actData);
				}else{
					temp.push(null);
				}
			}
		}
		
		private function cloneTemplate(template:Vector.<Vector>):Vector.<ActionModel>
		{
			if(template == null) return null;
			var temp:Vector.<ActionModel> = new Vector.<ActionModel>();
			var list:Vector.<CutModel> = null;
			var cutMode:CutModel = null;
			for(var i:uint = 0;i < template.length;i++){
				list = template[i] as Vector.<CutModel>;
				if(list){
					var actData:ActionModel = new ActionModel();
					actData.action = i;
					for(var j:uint = 0;j < list.length;j++){
						cutMode = list[j] as CutModel;
						var dirData:ActionModel = new ActionModel();
						dirData.direction = j;
						for(var k:uint = 0;k < cutMode.length;k++){
							var mode:ActionModel = new ActionModel();
							mode.action = i;
							mode.direction = j;
							mode.frame = k;
							mode.bitmap = new Bitmap(cutMode.getCloneData(k));
							dirData.addModel(mode);
						}
						actData.addModel(dirData);
					}
					temp.push(actData);
				}else{
					temp.push(null);
				}
			}
			return temp;
		}
		
		/**
		 * 根据数据的UID获取显示对象
		 * */
		private function getViewerByUID(uid:String):DressViewer
		{
			var obj:Object = null;
			for(var i:uint = 0;i < loadedList.length;i++){
				obj = loadedList[i];
				if(obj.uid == uid){
					return obj.viewer as DressViewer;
				}
			}
			return null;
		}
		
		/**
		 * 根据数据的UID获取数据模型
		 * */
		private function getModelByUID(uid:String):DressModel
		{
			var obj:Object = null;
			for(var i:uint = 0;i < loadedList.length;i++){
				obj = loadedList[i];
				if(obj.uid == uid){
					return obj.model as DressModel;
				}
			}
			return null;
		}
	}
}